﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Xml
Imports System.Windows.Forms

Namespace DoAnQuanLyBanHang.General
    Public Class ThamSo
        Public Shared DatabaseName As [String]
    End Class
#Region "Load form main"
    Public NotInheritable Class LoadMain
        Private Sub New()
        End Sub
        Public Shared STT As Integer = 1

#Region "goi form quan ly"
        Shared m_FrmQuyen As frmQuyen = Nothing

        Public Shared Sub HienThiQuyen()
            If m_FrmQuyen Is Nothing OrElse m_FrmQuyen.IsDisposed Then
                m_FrmQuyen = New frmQuyen()
                m_FrmQuyen.MdiParent = frmMain.ActiveForm
                m_FrmQuyen.Show()
            Else
                m_FrmQuyen.Activate()
            End If
        End Sub

        Shared m_FrmPhanQuyen As frmPhanQuyen = Nothing
        Public Shared Sub HienThiPhanQuyen()
            If m_FrmPhanQuyen Is Nothing OrElse m_FrmPhanQuyen.IsDisposed Then
                m_FrmPhanQuyen = New frmPhanQuyen()
                m_FrmPhanQuyen.MdiParent = frmMain.ActiveForm
                m_FrmPhanQuyen.Show()
            Else
                m_FrmPhanQuyen.Activate()
            End If
        End Sub

        Shared m_FrmKhachHang As frmKhachHang = Nothing
        Public Shared Sub HienThiKhachHang()
            If m_FrmKhachHang Is Nothing OrElse m_FrmKhachHang.IsDisposed Then
                m_FrmKhachHang = New frmKhachHang()
                m_FrmKhachHang.MdiParent = frmMain.ActiveForm
                m_FrmKhachHang.Show()
            Else
                m_FrmKhachHang.Activate()
            End If
        End Sub

        Shared m_FrmNhanVien As frmNhanVien = Nothing
        Public Shared Sub HienThiNhanVien()
            If m_FrmNhanVien Is Nothing OrElse m_FrmNhanVien.IsDisposed Then
                m_FrmNhanVien = New frmNhanVien()
                m_FrmNhanVien.MdiParent = frmMain.ActiveForm
                m_FrmNhanVien.Show()
            Else
                m_FrmNhanVien.Activate()
            End If
        End Sub

        Shared m_FrmQuayGiaoDich As frmQuayGiaoDich = Nothing
        Public Shared Sub HienThiQuayGiaoDich()
            If m_FrmQuayGiaoDich Is Nothing OrElse m_FrmQuayGiaoDich.IsDisposed Then
                m_FrmQuayGiaoDich = New frmQuayGiaoDich()
                m_FrmQuayGiaoDich.MdiParent = frmMain.ActiveForm
                m_FrmQuayGiaoDich.Show()
            Else
                m_FrmQuayGiaoDich.Activate()
            End If
        End Sub

        Shared m_FrmLoaiTien As frmLoaiTien = Nothing
        Public Shared Sub HienThiLoaiTien()
            If m_FrmLoaiTien Is Nothing OrElse m_FrmLoaiTien.IsDisposed Then
                m_FrmLoaiTien = New frmLoaiTien()
                m_FrmLoaiTien.MdiParent = frmMain.ActiveForm
                m_FrmLoaiTien.Show()
            Else
                m_FrmLoaiTien.Activate()
            End If
        End Sub

        Shared m_FrmLoaiGiaoDich As frmLoaiGiaoDich = Nothing
        Public Shared Sub HienThiLoaiGiaoDich()
            If m_FrmLoaiGiaoDich Is Nothing OrElse m_FrmLoaiGiaoDich.IsDisposed Then
                m_FrmLoaiGiaoDich = New frmLoaiGiaoDich()
                m_FrmLoaiGiaoDich.MdiParent = frmMain.ActiveForm
                m_FrmLoaiGiaoDich.Show()
            Else
                m_FrmLoaiGiaoDich.Activate()
            End If
        End Sub

        Shared m_FrmNhaCungCap As frmNhaCungCap = Nothing
        Public Shared Sub HienThiNhaCungCap()
            If m_FrmNhaCungCap Is Nothing OrElse m_FrmNhaCungCap.IsDisposed Then
                m_FrmNhaCungCap = New frmNhaCungCap()
                m_FrmNhaCungCap.MdiParent = frmMain.ActiveForm
                m_FrmNhaCungCap.Show()
            Else
                m_FrmNhaCungCap.Activate()
            End If
        End Sub

        Shared m_FrmThemMatHang As frmThemMatHang = Nothing
        Public Shared Sub HienThiMatHang()
            If m_FrmThemMatHang Is Nothing OrElse m_FrmThemMatHang.IsDisposed Then
                m_FrmThemMatHang = New frmThemMatHang()
                m_FrmThemMatHang.MdiParent = frmMain.ActiveForm
                m_FrmThemMatHang.Show()
            Else
                m_FrmThemMatHang.Activate()
            End If
        End Sub

        Shared m_FrmDatHang As frmDatHang = Nothing
        Public Shared Sub HienThiDatHang()
            If m_FrmDatHang Is Nothing OrElse m_FrmDatHang.IsDisposed Then
                m_FrmDatHang = New frmDatHang()
                m_FrmDatHang.MdiParent = frmMain.ActiveForm
                m_FrmDatHang.Show()
            Else
                m_FrmDatHang.Activate()
            End If
        End Sub

        Shared m_FrmXuatHang As frmXuatHang = Nothing
        Public Shared Sub HienThiXuatHang()
            If m_FrmXuatHang Is Nothing OrElse m_FrmXuatHang.IsDisposed Then
                m_FrmXuatHang = New frmXuatHang()
                m_FrmXuatHang.MdiParent = frmMain.ActiveForm
                m_FrmXuatHang.Show()
            Else
                m_FrmXuatHang.Activate()
            End If
        End Sub

        Shared m_FrmNhomNguoiDung As frmNhomNguoiDung = Nothing
        Public Shared Sub HienThiNhomNguoiDung()
            If m_FrmNhomNguoiDung Is Nothing OrElse m_FrmNhomNguoiDung.IsDisposed Then
                m_FrmNhomNguoiDung = New frmNhomNguoiDung()
                m_FrmNhomNguoiDung.MdiParent = frmMain.ActiveForm
                m_FrmNhomNguoiDung.Show()
            Else
                m_FrmNhomNguoiDung.Activate()
            End If
        End Sub

        Shared m_FrmPhanNhomNguoiDung As frmPhanNhomNguoiDung = Nothing
        Public Shared Sub HienThiPhanNhomNguoiDung()
            If m_FrmPhanNhomNguoiDung Is Nothing OrElse m_FrmPhanNhomNguoiDung.IsDisposed Then
                m_FrmPhanNhomNguoiDung = New frmPhanNhomNguoiDung()
                m_FrmPhanNhomNguoiDung.MdiParent = frmMain.ActiveForm
                m_FrmPhanNhomNguoiDung.Show()
            Else
                m_FrmPhanNhomNguoiDung.Activate()
            End If
        End Sub
#End Region

#Region "hiện thị form thống kê:"

        Shared m_FrmDSNhanVien As frmDSNhanVien = Nothing
        Public Shared Sub HienThiDSNhanVien()
            If m_FrmDSNhanVien Is Nothing OrElse m_FrmDSNhanVien.IsDisposed Then
                m_FrmDSNhanVien = New frmDSNhanVien()
                m_FrmDSNhanVien.MdiParent = frmMain.ActiveForm
                m_FrmDSNhanVien.Show()
            Else
                m_FrmDSNhanVien.Activate()
            End If
        End Sub

        Shared m_FrmDSMatHang As frmDSMatHang = Nothing
        Public Shared Sub HienThiDSMatHang()
            If m_FrmDSMatHang Is Nothing OrElse m_FrmDSMatHang.IsDisposed Then
                m_FrmDSMatHang = New frmDSMatHang()
                m_FrmDSMatHang.MdiParent = frmMain.ActiveForm
                m_FrmDSMatHang.Show()
            Else
                m_FrmDSMatHang.Activate()
            End If
        End Sub

        Shared m_FrmDoanhThu As frmDoanhThu = Nothing
        Public Shared Sub HienThiDoanhThu()
            If m_FrmDoanhThu Is Nothing OrElse m_FrmDoanhThu.IsDisposed Then
                m_FrmDoanhThu = New frmDoanhThu()
                m_FrmDoanhThu.MdiParent = frmMain.ActiveForm
                m_FrmDoanhThu.Show()
            Else
                m_FrmDoanhThu.Activate()
            End If
        End Sub

        Shared m_FrmTonKho As frmTonKho = Nothing
        Public Shared Sub HienThiTonKho()
            If m_FrmTonKho Is Nothing OrElse m_FrmTonKho.IsDisposed Then
                m_FrmTonKho = New frmTonKho()
                m_FrmTonKho.MdiParent = frmMain.ActiveForm
                m_FrmTonKho.Show()
            Else
                m_FrmTonKho.Activate()
            End If
        End Sub
#End Region

#Region "hiển thị menu tìm kiếm"
        Shared m_FrmTimKiemNhanVien As frmTimKiemNhanVien = Nothing
        Public Shared Sub HienThiTimKiemNhanVien()
            If m_FrmTimKiemNhanVien Is Nothing OrElse m_FrmTimKiemNhanVien.IsDisposed Then
                m_FrmTimKiemNhanVien = New frmTimKiemNhanVien()
                m_FrmTimKiemNhanVien.MdiParent = frmMain.ActiveForm
                m_FrmTimKiemNhanVien.Show()
            Else
                m_FrmTimKiemNhanVien.Activate()
            End If
        End Sub

        Shared m_FrmTimKiemMatHang As frmTimKiemMatHang = Nothing
        Public Shared Sub HienThiTimKiemMatHang()
            If m_FrmTimKiemMatHang Is Nothing OrElse m_FrmTimKiemMatHang.IsDisposed Then
                m_FrmTimKiemMatHang = New frmTimKiemMatHang()
                m_FrmTimKiemMatHang.MdiParent = frmMain.ActiveForm
                m_FrmTimKiemMatHang.Show()
            Else
                m_FrmTimKiemMatHang.Activate()
            End If
        End Sub
        Shared m_FrmTimNhaCungCap As frmTimNhaCungCap = Nothing
        Public Shared Sub HienThiTimNhaCungCap()
            If m_FrmTimNhaCungCap Is Nothing OrElse m_FrmTimNhaCungCap.IsDisposed Then
                m_FrmTimNhaCungCap = New frmTimNhaCungCap()
                m_FrmTimNhaCungCap.MdiParent = frmMain.ActiveForm
                m_FrmTimNhaCungCap.Show()
            Else
                m_FrmTimNhaCungCap.Activate()
            End If
        End Sub

        Shared m_FrmTimKiemKhachHang As frmTimKiemKhachHang = Nothing
        Public Shared Sub HienThiTimKiemKhachHang()
            If m_FrmTimKiemKhachHang Is Nothing OrElse m_FrmTimKiemKhachHang.IsDisposed Then
                m_FrmTimKiemKhachHang = New frmTimKiemKhachHang()
                m_FrmTimKiemKhachHang.MdiParent = frmMain.ActiveForm
                m_FrmTimKiemKhachHang.Show()
            Else
                m_FrmTimKiemKhachHang.Activate()
            End If
        End Sub
#End Region

#Region "hiển thị menu lập phiếu"

        Shared m_FrmDonDatHang As frmDonDatHang = Nothing
        Public Shared Sub HienThiDonDatHang()
            If m_FrmDonDatHang Is Nothing OrElse m_FrmDonDatHang.IsDisposed Then
                m_FrmDonDatHang = New frmDonDatHang()
                m_FrmDonDatHang.MdiParent = frmMain.ActiveForm
                m_FrmDonDatHang.Show()
            Else
                m_FrmDonDatHang.Activate()
            End If
        End Sub

        Shared m_FrmPhieuXuatHang As frmPhieuXuatHang = Nothing
        Public Shared Sub HienThiPhieuXuatHang()
            If m_FrmPhieuXuatHang Is Nothing OrElse m_FrmPhieuXuatHang.IsDisposed Then
                m_FrmPhieuXuatHang = New frmPhieuXuatHang()
                m_FrmPhieuXuatHang.MdiParent = frmMain.ActiveForm
                m_FrmPhieuXuatHang.Show()
            Else
                m_FrmPhieuXuatHang.Activate()
            End If
        End Sub
        Shared m_FrmNhapHang As frmNhapHang = Nothing
        Public Shared Sub HienThiPhieuNhapHang()
            If m_FrmNhapHang Is Nothing OrElse m_FrmNhapHang.IsDisposed Then
                m_FrmNhapHang = New frmNhapHang()
                m_FrmNhapHang.MdiParent = frmMain.ActiveForm
                m_FrmNhapHang.Show()
            Else
                m_FrmNhapHang.Activate()
            End If
        End Sub
#End Region
    End Class
#End Region

#Region "Ket not DBMS"
    Public Class DBMS

    End Class
#End Region
#Region "Xu ly tap tin Xml"
    Public Class XML
        Public Shared Function ReadXml(fileName As [String]) As XmlDocument
            Dim XmlDocument As New XmlDocument()
            Try
                XmlDocument.Load(fileName)
            Catch
                MessageBox.Show("Không đọc được hoặc không tồn tại tập tin cấu hình " & fileName, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            End Try
            Return XmlDocument
        End Function

        Public Shared Sub WriteXml(fileName As [String], serverName As [String], dataBase As [String], costatus As [String])
            Dim XmlWrite As New XmlTextWriter(fileName, Nothing)
            XmlWrite.Formatting = Formatting.Indented

            XmlWrite.WriteStartDocument()
            XmlWrite.WriteComment("Thong so co ban:" & vbLf & vbTab & "costatus = true : quyen Windows" & vbLf & vbTab & "costatus = false: quyen SQL Server" & vbLf & vbTab & "servname: ten server" & vbLf & vbTab & "username: ten dang nhap he thong" & vbLf & vbTab & "password: mat khau dang nhap he thong" & vbLf & vbTab & "database: ten co so du lieu" & vbLf)
            XmlWrite.WriteStartElement("config")

            XmlWrite.WriteStartElement("costatus")
            XmlWrite.WriteString(costatus)
            XmlWrite.WriteEndElement()

            XmlWrite.WriteStartElement("servername")
            XmlWrite.WriteString(serverName)
            XmlWrite.WriteEndElement()

            XmlWrite.WriteStartElement("username")
            XmlWrite.WriteString("")
            XmlWrite.WriteEndElement()

            XmlWrite.WriteStartElement("password")
            XmlWrite.WriteString("")
            XmlWrite.WriteEndElement()

            XmlWrite.WriteStartElement("database")
            XmlWrite.WriteString(dataBase)
            XmlWrite.WriteEndElement()

            XmlWrite.WriteEndElement()
            XmlWrite.WriteEndDocument()

            XmlWrite.Close()
        End Sub

        Public Shared Sub XMLWriter(fileName As [String], serverName As [String], userName As [String], passWord As [String], dataBase As [String], costatus As [String])
            Dim xmlW As New XmlTextWriter(fileName, Nothing)
            xmlW.Formatting = Formatting.Indented

            xmlW.WriteStartDocument()
            xmlW.WriteComment(vbLf & "Khong duoc thay doi noi dung file nay!" & vbLf & "Thong so co ban:" & vbLf & vbTab & "costatus = true : quyen Windows" & vbLf & vbTab & "costatus = false: quyen SQL Server" & vbLf & vbTab & "servname: ten server" & vbLf & vbTab & "username: ten dang nhap he thong" & vbLf & vbTab & "password: mat khau dang nhap he thong" & vbLf & vbTab & "database: ten co so du lieu" & vbLf)
            xmlW.WriteStartElement("config")

            xmlW.WriteStartElement("costatus")
            xmlW.WriteString(costatus)
            xmlW.WriteEndElement()

            xmlW.WriteStartElement("servername")
            xmlW.WriteString(serverName)
            xmlW.WriteEndElement()

            xmlW.WriteStartElement("username")
            xmlW.WriteString(userName)
            xmlW.WriteEndElement()

            xmlW.WriteStartElement("password")
            xmlW.WriteString(passWord)
            xmlW.WriteEndElement()

            xmlW.WriteStartElement("database")
            xmlW.WriteString(dataBase)
            xmlW.WriteEndElement()

            xmlW.WriteEndElement()
            xmlW.WriteEndDocument()

            xmlW.Close()
        End Sub

    End Class
#End Region
#Region "Quy dinh"
    Public Class QuyDinh
        Public Function LaySTT(autoNum As Integer) As [String]
            If autoNum < 10 Then
                Return "00000" & autoNum
            ElseIf autoNum >= 10 AndAlso autoNum < 100000 Then
                Return "0000" & autoNum

            ElseIf autoNum >= 1000 AndAlso autoNum < 10000 Then
                Return "000" & autoNum
            ElseIf autoNum >= 10000 AndAlso autoNum < 100000 Then
                Return "00" & autoNum
            ElseIf autoNum >= 10000 AndAlso autoNum < 100000 Then
                Return "0" & autoNum
            ElseIf autoNum >= 10000 AndAlso autoNum < 100000 Then
                Return "" & autoNum
            Else
                Return ""
            End If
        End Function

        Private m_DiaChi As [String]
        Public Property DiaChi() As [String]
            Get
                Return m_DiaChi
            End Get
            Set(value As [String])
                m_DiaChi = value
            End Set
        End Property
    End Class
#End Region
End Namespace